package com.fancy.learncenter.activity;

import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.ReumeInfoDataBean;
import com.fancy.learncenter.bean.VitaDetailDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.DimenUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;
import com.fancy.learncenter.view.LPopWindow;
import com.fancy.learncenter.view.PopupWindowCutPic;
import com.fancy.learncenter.view.PopupWindowEducationType;
import com.fancy.learncenter.view.PopupWindowExperienceType;
import com.fancy.learncenter.view.PopupWindowSalaryType;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.model.TResult;
import com.superservice.lya.R;

import java.io.File;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.addapp.pickers.common.LineConfig;
import cn.addapp.pickers.listeners.OnItemPickListener;
import cn.addapp.pickers.picker.DatePicker;
import cn.addapp.pickers.picker.SinglePicker;

public class PersonVitaActivity extends TakePhotoActivity {

    @Bind(R.id.name)
    EditText name;
    @Bind(R.id.sex)
    TextView sex;
    @Bind(R.id.birthday)
    TextView birthday;
    @Bind(R.id.iamge_user)
    SimpleDraweeView iamgeUser;
    @Bind(R.id.education_tv)
    TextView educationTv;
    @Bind(R.id.experience_tv)
    TextView experienceTv;
    @Bind(R.id.mobile_tv)
    EditText mobileTv;
    @Bind(R.id.sex_ll)
    RelativeLayout sexLl;
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;


    PopupWindowEducationType educationType;

    PopupWindowExperienceType experienceType;

    PopupWindowCutPic popupWindowCutPic;

    SinglePicker<String> xingbie_picker;//性别选择器

    String resumeId;

    VitaDetailDataBean vitaDetailDataBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_person_vita);
        ButterKnife.bind(this);
        title.setText("基本资料");

        resumeId = getIntent().getStringExtra("resumeId");

        iamgeUser.setImageURI(SPUtils.getUserIcon());

        initPicker();
        initBirthDayPicker();

        takePhoto = getTakePhoto();
        takePhoto.onEnableCompress(Utils.takePhotoCompress(), true);

        getData();
    }

    TakePhoto takePhoto;
    String educationId;

    @OnClick({R.id.education_ll, R.id.sex_ll, R.id.submit, R.id.back, R.id.iamge_user, R.id.shengri, R.id.experience_ll, R.id.mobile_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
            case R.id.submit:
                editPersonVita();
                break;
            case R.id.education_ll:

                if (educationType == null) {
                    educationType = new PopupWindowEducationType(this, sexLl);
                    educationType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
                        @Override
                        public void cancleCallBack() {

                        }

                        @Override
                        public void selecCallBack(String content, String id) {
                            educationTv.setText(content);
                            educationId = content;
                        }

                    });
                }
                educationType.showPopupWindow();

                break;
            case R.id.experience_ll:


                if (experienceType == null) {
                    experienceType = new PopupWindowExperienceType(this, sexLl);

                    experienceType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
                        @Override
                        public void cancleCallBack() {

                        }

                        @Override
                        public void selecCallBack(String content, String id) {
                            experienceTv.setText(content);

                            experienceId = content;
                        }

                    });


                }
                experienceType.showPopupWindow();

                break;
            case R.id.mobile_ll:
                break;
            case R.id.sex_ll:
                xingbie_picker.show();
                break;
            case R.id.iamge_user:
//
//                if (popupWindowCutPic == null) {
//                    popupWindowCutPic = new PopupWindowCutPic(sexLl);
//                    popupWindowCutPic.setOnClickListener(new PopupWindowCutPic.CutPicCallBack() {
//                        @Override
//                        public void choose() {
//                            takePhoto.onPickFromGalleryWithCrop(Utils.getCutPicPath(), Utils.getCutPicCropOptions());
//                        }
//
//                        @Override
//                        public void cancel() {
//
//                        }
//
//                        @Override
//                        public void camera() {
//                            takePhoto.onPickFromCaptureWithCrop(Utils.getCutPicPath(), Utils.getCutPicCropOptions());
//                        }
//
//                        @Override
//                        public void see() {
//
//                        }
//                    });
//
//                }
//
//                popupWindowCutPic.isShow();
                break;
            case R.id.shengri:
                datePicker.show();

                break;
        }
    }


    String urlPath;

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);

        if (result.getImage() != null && !TextUtils.isEmpty(result.getImage().getCompressPath())) {
            urlPath = result.getImage().getCompressPath();
            iamgeUser.setImageURI(Uri.fromFile(new File(result.getImage().getCompressPath())));
        }
    }


    //初始化选择器
    public void initPicker() {
        xingbie_picker = new SinglePicker<>(this, new String[]{"男", "女"});
        xingbie_picker.setCanLoop(true);
        xingbie_picker.setTopBackgroundColor(0xFFEEEEEE);
        xingbie_picker.setTopHeight(50);
        xingbie_picker.setTopLineColor(0xFF33B5E5);
        xingbie_picker.setTopLineHeight(1);
        xingbie_picker.setTitleText("请选择性别");
        xingbie_picker.setTitleTextColor(0xFF999999);
        xingbie_picker.setTitleTextSize(14);
        xingbie_picker.setOffset(1);
        xingbie_picker.setCancelTextColor(0xFF33B5E5);
        xingbie_picker.setCancelTextSize(15);
        xingbie_picker.setSubmitTextColor(0xFF33B5E5);
        xingbie_picker.setSubmitTextSize(15);
        xingbie_picker.setSelectedTextColor(getResources().getColor(R.color.colorPrimary));
        xingbie_picker.setUnSelectedTextColor(0xFF999999);
        LineConfig config = new LineConfig();
        config.setColor(getResources().getColor(R.color.colorPrimary));//线颜色
        config.setAlpha(140);//线透明度
        config.setRatio((float) (1.0 / 8.0));//线比率
        xingbie_picker.setLineConfig(config);
        xingbie_picker.setItemWidth(200);
        xingbie_picker.setBackgroundColor(0xFFE1E1E1);
        final String sexStr = sex.getText().toString();
        if (!TextUtils.isEmpty(sexStr)) {
            xingbie_picker.setSelectedItem(sexStr);
        }
        xingbie_picker.setOnItemPickListener(new OnItemPickListener<String>() {
            @Override
            public void onItemPicked(int index, String item) {
                sex.setText(item);
            }
        });
    }

    DatePicker datePicker;//生日选择器

    public void initBirthDayPicker() {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int date = c.get(Calendar.DATE);
        datePicker = new DatePicker(this);
        datePicker.setCanLoop(false);
        datePicker.setTopHeight(50);
        datePicker.setTopLineColor(0xFF33B5E5);
        datePicker.setTopLineHeight(1);
        datePicker.setCancelTextColor(0xFF33B5E5);
        datePicker.setCancelTextSize(15);
        datePicker.setSubmitTextColor(0xFF33B5E5);
        datePicker.setSubmitTextSize(15);
        datePicker.setWheelModeEnable(true);
        datePicker.setHeight(DimenUtil.dip2px(250));
        datePicker.setTopPadding(25);
        datePicker.setRangeStart(1899, 1, 1);
        datePicker.setRangeEnd(year, month, date);
        datePicker.setSelectedItem(1999, 9, 9);
        datePicker.setWeightEnable(true);
        datePicker.setBackgroundColor(0xFFE1E1E1);
        datePicker.setSelectedTextColor(getResources().getColor(R.color.colorPrimary));
        LineConfig config = new LineConfig();
        config.setColor(getResources().getColor(R.color.colorPrimary));//线颜色
        config.setAlpha(140);//线透明度
        config.setRatio((float) (1.0 / 8.0));//线比率
        datePicker.setLineConfig(config);
        datePicker.setTitleTextColor(0xFF999999);
        datePicker.setTitleTextSize(14);
        datePicker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                birthday.setText(year + "-" + month + "-" + day);
            }
        });
        datePicker.setOnWheelListener(new DatePicker.OnWheelListener() {
            @Override
            public void onYearWheeled(int index, String year) {
                datePicker.setTitleText(year + "-" + datePicker.getSelectedMonth() + "-" + datePicker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                datePicker.setTitleText(datePicker.getSelectedYear() + "-" + month + "-" + datePicker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                datePicker.setTitleText(datePicker.getSelectedYear() + "-" + datePicker.getSelectedMonth() + "-" + day);
            }
        });
    }


    private void getData() {

        HashMap map = new HashMap();
        map.put("resumeId", resumeId);
        map.put("token", SPUtils.getToken());
        map.put("userId", SPUtils.getUserID());
        HttpMehtod.getInstance().resumeInfo(map, new IdeaObserver<BaseDataBean<ReumeInfoDataBean>>() {


            @Override
            public void onSuccess(BaseDataBean<ReumeInfoDataBean> reumeInfoDataBeanBaseDataBean) {
                birthday.setText(reumeInfoDataBeanBaseDataBean.getData().getBirthday());

                educationTv.setText(reumeInfoDataBeanBaseDataBean.getData().getEducation());

                educationId = reumeInfoDataBeanBaseDataBean.getData().getEdu_id() + "";

                experienceTv.setText(reumeInfoDataBeanBaseDataBean.getData().getWork_year());

                experienceId = reumeInfoDataBeanBaseDataBean.getData().getWork_id() + "";

                mobileTv.setText(reumeInfoDataBeanBaseDataBean.getData().getMobile());

                name.setText(reumeInfoDataBeanBaseDataBean.getData().getReal_name());

            }

            @Override
            public void onFail(int errorCode) {

            }
        });


    }


    private String experienceId = "";

    private void editPersonVita() {
        Map map = new HashMap();

        map.put("birthday", birthday.getText().toString());


        map.put("resumeId", resumeId);

        map.put("education", educationTv.getText().toString());

        map.put("work_year", experienceTv.getText().toString());

        map.put("mobile", mobileTv.getText().toString());

        map.put("realName", name.getText().toString());


        HttpMehtod.getInstance().editPersonVita(map, new IdeaObserver<BaseDataBean>() {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                ToastUtil.show("资料已更新");
                finish();
            }

            @Override
            public void onFail(int errorCode) {

            }
        });

    }
}
